package com.sina.interfaces.core;

import com.sina.interfaces.core.exception.InterfaceCustomException;
import org.hibernate.exception.DataException;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2017-09-21-11:21
 */
public interface BaseService {
    Serializable create(Object entity);


    void update(Object entity);

    void createOrUpdate(Object entity);

    <T> T get(Class<T> entityClass, Serializable id);

    void delete(Object entity);

    void delete(Collection entities);

    <T> boolean delete(Class<T> entityClass, Serializable id);

    <T> List<T> list(Class<T> clazz, int page, int max);

    /**
     * @throws DataException - 当 id 为 null,或 数据不存在时
     * @see #get(Class, Serializable)
     */
    <T> T getWithException(Class<T> entityClass, Serializable id) throws DataException;

    /**
     * @param entityClass (not null)
     * @param id          (not null)
     * @return true 表示存在
     */
    boolean isExists(Class<?> entityClass, Serializable id);

    /**
     * @param entityClass (not null)
     * @param id          若为 null 则无动作
     * @throws InterfaceCustomException - 如果存在则抛出异常
     * @see #isExists(Class, Serializable)
     */
    void isExistsWithException(Class<?> entityClass, Serializable id) throws InterfaceCustomException;

    default Class<?> getEntityClass() {
        return Object.class;
    }
}
